﻿/*
 * @file: ExceptionWriter.cs
 */
using System;
using System.IO;
using System.Web;

/// <summary>
/// Summary description for ExceptionWriter
/// </summary>
public class ExceptionWriter
{
    public Exception CurrentException { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="exception">Exception</param>
	public ExceptionWriter(Exception exception)
	{
        this.CurrentException = exception;
	}

    /// <summary>
    /// Write exception to log file
    /// </summary>
    public void WriteException()
    {
        StreamWriter writer = null;
        string logFile = "log_" + DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year;
        string exceptionFilePath = HttpContext.Current.Server.MapPath("~/App_ExceptionLogs/logs/" + logFile);
        string currentTime = DateTime.Now.ToShortTimeString();
       
        try
        {
            if (!File.Exists(exceptionFilePath))
            {
                writer = File.CreateText(exceptionFilePath);
                writer.Close();
            }
            writer = new StreamWriter(exceptionFilePath, true);
            writer.WriteLine(currentTime + ":" + CurrentException.Message);
            writer.WriteLine("DETAILS:" + CurrentException.StackTrace);
        }
        catch (Exception ex)
        {
            
        }
        finally {
            if(writer != null)
                writer.Close();
        }
    }
}
